package com.nf.health.app.activity.home;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.home.bean.SatisfactionSurveyQuestionBean;
import com.nf.health.app.activity.home.view.BaseSelectView;
import com.nf.health.app.activity.home.view.MultipleSelectView;
import com.nf.health.app.activity.home.view.RichTextView;
import com.nf.health.app.activity.home.view.SimpleTextView;
import com.nf.health.app.activity.home.view.SingleSelectView;
import com.nf.health.app.http.Act;
import com.nf.health.app.utils.Util;
import com.nf.health.app.utils.ViewUtil;


import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by he_pan on 2017/4/10.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author he_pan
 * @Description
 */

public class SatisfactionSurveyQuestionActivity extends BaseActivity {
    public static String HID = "com.nf.hid";
    @Bind(R.id.titlebar_back_view)
    ImageView titlebarBackView;
    @Bind(R.id.titlebar_title_tv)
    TextView titlebarTitleTv;
    @Bind(R.id.titlebar_right_view)
    ImageView titlebarRightView;
    @Bind(R.id.titlebar_right_tv)
    TextView titlebarRightTv;
    @Bind(R.id.btn_commit)
    Button btnCommit;
    @Bind(R.id.ll_question_list)
    LinearLayout llQuestionList;
    @Bind(R.id.tv_name)
    TextView tvName;
    private String hid = "";
    private List<SatisfactionSurveyQuestionBean> mQuestionList;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_satisfaction_survey_question);
        ButterKnife.bind(this);
        initView();
        initListener();
        loadData();
    }

    private void initView() {
        hid = getIntent().getStringExtra(HID);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "满意度调查");
        tvName.setText("尊敬的" + Util.getNameOrNick());
        btnCommit.setVisibility(View.GONE);
    }

    private void initListener() {
        btnCommit.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void loadData() {
        provider.getSatisfactionSurveyQuestion(hid);
    }

    /**
     * 显示问卷
     *
     * @param list
     */
    private void setUpViewByList(List<SatisfactionSurveyQuestionBean> list) {
        llQuestionList.removeAllViews();
        String type = "";
        View v = new View(this);
        for (int i = 0; i < list.size(); i++) {
            type = list.get(i).getType();
            switch (type) {
                case "1"://单选
                    v = new SingleSelectView(this, mQuestionList.get(i), i);
                    break;
                case "2"://多选
                    v = new MultipleSelectView(this, mQuestionList.get(i), i);
                    break;
                case "3"://多文本
                    v = new RichTextView(this, mQuestionList.get(i), i);
                    break;
                case "4"://单文本
                    v = new SimpleTextView(this, mQuestionList.get(i), i);
                    break;
                default:
                    break;
            }

            llQuestionList.addView(v);
        }
    }


    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (Act.Flag.FLAG_GET_SATISFT_QUESTION_LIST.equals(actionName)) {
            btnCommit.setVisibility(View.VISIBLE);
            mQuestionList = (List<SatisfactionSurveyQuestionBean>) object;
            setUpViewByList(mQuestionList);
        } else if (Act.Flag.FLAG_COMMIT_SATISFT_QUESTION_ANSWER.equals(actionName)) {
            showToast("问卷调查成功提交,感谢您的参与");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            }, 1500);
        }
    }

    @Override
    public void handleActionError(String actionName, String response, VolleyError error) {
        super.handleActionError(actionName, response, error);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_commit:
                commitQuestionnaire();
                break;
        }
    }

    /**
     * 提交问卷
     */
    private void commitQuestionnaire() {
        HashMap<String, String> answerMap = new HashMap<>();
        for (int i = 0; i < llQuestionList.getChildCount(); i++) {
            View v = llQuestionList.getChildAt(i);
            if (v instanceof BaseSelectView) {
                BaseSelectView baseView = (BaseSelectView) v;
                answerMap.put(baseView.getPosition(), baseView.getSurveyAnswer());
            }
        }
        String answer = com.alibaba.fastjson.JSONObject.toJSONString(answerMap);
        provider.commitSatisfactionSurveyAnswer(answer,hid);
    }

}
